﻿using ImageUploader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageUploaderServer.core
{
    class Listener
    {
        #region tcplistener的方式，先留着备用
        //listener = new TcpListener(IPAddress.Parse(txt_localIP.Text), int.Parse(txt_localPort.Text));
        //listener.Start();
        //ShowMsg.showMsg(listb_InfoList, "服务开始监听");
        //th = new Thread(ReceiveMsg);
        //th.Start();
        //th.IsBackground = true;
        //boolStop = false;
        #endregion
        TcpListener listener;
        Boolean boolStop = false;
        ListBox listb_ClientList;
        ListBox listb_InfoList;
        //tcplistener的方式接收数据
        public void ReceiveMsg(ListBox info, ListBox clients)
        {
            listb_ClientList = clients;
            listb_InfoList = info;
            while (true)
            {
                if (!listener.Pending())
                {
                    Thread.Sleep(100);
                    //为了避免每次都被tcpListener.AcceptTcpClient()阻塞线程，添加了此判断
                    //当没有连接请求时，休息1s，有了请求再执行到tcpListener.AcceptTcpClient() 
                }
                else
                {
                    try
                    {
                        int size = 0;
                        int len = 0;
                        TcpClient client = listener.AcceptTcpClient();
                        if (client.Connected)
                        {
                            //向列表控件中添加一个客户端的Ip和端口，作为发送时客户的唯一标识
                            listb_ClientList.Items.Add(client.Client.RemoteEndPoint);
                            ShowMsg.showMsg(listb_InfoList, "客户端连接成功" + client.Client.RemoteEndPoint.ToString());
                        }
                        NetworkStream stream = client.GetStream();
                        if (stream != null)
                        {
                            string Path = @"..\..\files";//获得用户保存文件的路径
                            string fileSavePath = Path + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".bmp";
                            FileStream fs = new FileStream(fileSavePath, FileMode.Create, FileAccess.Write);
                            byte[] buffer = new byte[512];
                            while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                fs.Write(buffer, 0, size);
                                len += size;
                            }
                            fs.Flush();
                            fs.Close();
                            stream.Flush();
                            stream.Close();
                            client.Close();
                            ShowMsg.showMsg(listb_InfoList, "文件接受成功" + fileSavePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMsg.showMsg(listb_InfoList, "出现异常：" + ex.Message);
                    }
                }
                if (boolStop)
                { break; }
            }
        }

    }
}
